package com.rslai.commons.http.tools;

import org.apache.http.impl.client.DefaultHttpClient;

/**
 * 拦截器
 * Created by lrs on 2018/8/7.
 */
public class Interceptors {

    /**
     * 将 debug 拦截器添加到 client 中
     * @param client HttpClient
     * @param open 是否开启 debug 拦截器
     */
    public static void debug(DefaultHttpClient client, boolean open) {
        client.removeRequestInterceptorByClass(DebugInterceptor.class);
        client.removeResponseInterceptorByClass(DebugInterceptor.class);
        if (open) {
            client.addRequestInterceptor(DebugInterceptor.instance);
            client.addResponseInterceptor(DebugInterceptor.instance);
        }
    }

    /**
     * 将 gzip 拦截器添加到 client 中
     * @param client HttpClient
     * @param open 是否开启 gzip 拦截器
     */
    public static void gzip(DefaultHttpClient client, boolean open) {
        client.removeRequestInterceptorByClass(DeflateInterceptor.class);
        client.removeResponseInterceptorByClass(DeflateInterceptor.class);
        if (open) {
            client.addRequestInterceptor(DeflateInterceptor.instance);
            client.addResponseInterceptor(DeflateInterceptor.instance);
        }
    }

}
